<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
/*
	签到记录
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$return['params']=array();	
	//会员ID
	if(!empty($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}	
	//type
	$params['type']=intval($_RQ['type']);
	$return['params']['type']=$_RQ['type'];
	if(!empty($_RQ['name'])){
		$nameparams=array('uniacid'=>$_SESSION['uniacid']);
		$nameparams['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
		if($params['type']==0){
			$mids=pdo_getall('jy_kexie_user',$nameparams,'mid','mid');
		}else{
			$mids=pdo_getall('jy_kexie_worker',$nameparams,'mid','mid');
		}
		if($mids){
			$mids=array_keys($mids);
		}else{
			$mids=-1;
		}
		$params['mid']=$mids;
	}
	//时间范围
	if(!empty($_RQ['tstart']) && $_RQ['tstart'] !='1970-01-01 08:00'){
		$tstart=strtotime($_RQ['tstart']);
		$return['params']['tstart']=trim($_RQ['tstart']);
	}
	if(!empty($_RQ['tend']) && $_RQ['tend'] !='1970-01-01 08:00'){
		$tend=strtotime($_RQ['tend']);
		$return['params']['tend']=trim($_RQ['tend']);
	}
	$nowyear=date('Y');
	$year = $_RQ['year']?:date('Y');
	$tstart=strtotime($year.'-01-01');
	if($year==$nowyear){
		$params['createtime >']=$tstart;
	}else{
		$nextyear =$year+1;
		$tend=strtotime($nextyear.'-01-01');
		$params['createtime between']=array($tstart,$tend);
	}
	$return['params']['year']=$year;
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:30;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$total='';
	$orderby='id desc';
	$list=pdo_getslice('jy_kexie_sign',$params,array($pindex,$psize),$total,'','',$orderby);
	if($list){
		foreach($list as &$log){
			$log['date']=date('Y-m-d',$log['createtime']);
			$member=model('core/member')->detail($log['mid'],array('credit1','credit2','mobile','groupid','avatar','nickname'));
			$log['member']=$member;
			if($log['type']==0){
				$log['username']=pdo_getcolumn('jy_kexie_user',array('mid'=>$log['mid'],'uniacid'=>$_SESSION['uniacid']),'name');
			}else{
				$log['username']=pdo_getcolumn('jy_kexie_worker',array('mid'=>$log['mid'],'uniacid'=>$_SESSION['uniacid']),'name');
			}
			$log['pai']=pdo_get('jy_kexie_lesson_pai',array('id'=>$log['lesson_paiid']));
			$log['storeid']=$log['pai']['storeid'];
			$log['pai']['images']=json_decode($log['pai']['images'],true);
			$log['pai']['image']=$log['pai']['images'][0];
			$log['lessontitle']=pdo_getcolumn('jy_kexie_lesson',array('id'=>$log['pai']['lessonid']),'title');
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'签到记录'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'total'=>$total,'psize'=>$psize,'list'=>array(
			array('name'=>'mid','title'=>'会员ID','type'=>'string'),
			array('name'=>'username','title'=>'姓名','type'=>'string'),
			array('name'=>'lessontitle','title'=>'课程','type'=>'string'),
			array('name'=>'storeid','title'=>'所属学校','type'=>'select','options'=>'stores'),
			array('name'=>'date','title'=>'签到日期','type'=>'string'),
			array('name'=>'pai.tingnum','title'=>'签到人数','type'=>'string'),
			array('name'=>'pai.image','title'=>'签到照片','type'=>'image'),
			array('name'=>'createtime','title'=>'签到时间','type'=>'time'),
			array('name'=>'storetime','title'=>'分校签到时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'name','width'=>15,'title'=>'姓名','type'=>'string'),
			array('name'=>'year','width'=>10,'title'=>'所属年份','type'=>'select','options'=>'years'),
		)
	);
	$return['years']=array();
	$nowyear = date('Y');
	for($i=1;$i<=10;$i++){
		$now=$nowyear-(10-$i);
		$return['years'][]=array('id'=>$now,'title'=>$now);
	}
	for($i=1;$i<=2;$i++){
		$now=$nowyear+$i;
		$return['years'][]=array('id'=>$now,'title'=>$now);
	}
	$return['stores']=pdo_getall('jy_kexie_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	//type
	$params['type']=intval($_RQ['type']);
	$return['params']['type']=$_RQ['type'];
	if(!empty($_RQ['name'])){
		$nameparams=array('uniacid'=>$_SESSION['uniacid']);
		$nameparams['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
		if($params['type']==0){
			$mids=pdo_getall('jy_kexie_user',$nameparams,'mid','mid');
		}else{
			$mids=pdo_getall('jy_kexie_worker',$nameparams,'mid','mid');
		}
		if($mids){
			$mids=array_keys($mids);
		}else{
			$mids=-1;
		}
		$params['mid']=$mids;
	}
	//时间范围
	if(!empty($_RQ['tstart']) && $_RQ['tstart'] !='1970-01-01 08:00'){
		$tstart=strtotime($_RQ['tstart']);
		$return['params']['tstart']=trim($_RQ['tstart']);
	}
	if(!empty($_RQ['tend']) && $_RQ['tend'] !='1970-01-01 08:00'){
		$tend=strtotime($_RQ['tend']);
		$return['params']['tend']=trim($_RQ['tend']);
	}
	$nowyear=date('Y');
	$year = $_RQ['year']?:date('Y');
	$tstart=strtotime($year.'-01-01');
	if($year==$nowyear){
		$params['createtime >']=$tstart;
	}else{
		$nextyear =$year+1;
		$tend=strtotime($nextyear.'-01-01');
		$params['createtime between']=array($tstart,$tend);
	}
	$sign=pdo_getall('jy_kexie_sign',$params,'','','id desc');
	foreach($sign as &$log){
			$log['createtime']=date('Y-m-d',$log['createtime']);
			$log['storetime']=date('Y-m-d',$log['storetime']);
			$member=model('core/member')->detail($log['mid'],array('credit1','credit2','mobile','groupid','avatar','nickname'));
			$log['member']=$member;
			if($log['type']==0){
				$log['username']=pdo_getcolumn('jy_kexie_user',array('mid'=>$log['mid'],'uniacid'=>$_SESSION['uniacid']),'name');
			}else{
				$log['username']=pdo_getcolumn('jy_kexie_worker',array('mid'=>$log['mid'],'uniacid'=>$_SESSION['uniacid']),'name');
			}
			$log['pai']=pdo_get('jy_kexie_lesson_pai',array('id'=>$log['lesson_paiid']));
			$log['storeid']=$log['pai']['storeid'];
			$log['pai']['images']=json_decode($log['pai']['images'],true);
			$log['pai']['image']=$log['pai']['images'][0];
			$log['tingnum']=$log['pai']['tingnum'];
			$log['lessontitle']=pdo_getcolumn('jy_kexie_lesson',array('id'=>$log['pai']['lessonid']),'title');
			$log['storetitle']=pdo_getcolumn('jy_kexie_store',array('id'=>$log['storeid']),'title');
	}
	$header=array(
		'username'=>'姓名',
		'lessontitle'=>'课程',
		'storetitle'=>'所属学校',
		'tingnum'=>'签到人数',
		'createtime'=>'签到时间',
		'storetime'=>'学校签到时间',
	);
	cfc('tools')->export2excel($header,$sign,$year.'-签到表');
}